# ommbot/world.py
#
#

""" module to stuff object in that need to be globally available """

__copyright__ = 'this file is in the public domain'

import ommbot.partyline
import ommbot.eventhandler

datadir = None
plugs = None
fleet = None
log = None
users = None
config = None
partyline = ommbot.partyline.Partyline()
mainhandler = ommbot.eventhandler.Eventhandler()

def enable_config(datadir):
    import ommbot.config
    global config
    config = ommbot.config.Config(datadir)
    config.load()
    return config

def enable_logging(level):
    import ommbot.log
    global log
    log = ommbot.log.Log(level)
    return log

def enable_plugs(datadir, plugsdirs=['plugs']):
    import ommbot.plugins
    global plugs
    plugs = ommbot.plugins.Plugins(datadir)
    for i in plugsdirs:
        plugs.regplugins(i)
    return plugs

def enable_fleet(bot, datadir):
    import ommbot.fleet
    global fleet
    fleet = ommbot.fleet.Fleet(datadir)
    fleet.addbot(bot)
    return fleet

def add_cmnd(cmnd, func, perms):
    if plugs:
        plugs.cmnds.add(cmnd, func, perms)

def add_callback(cmnd, callback, precondition):
    if plugs:
        plugs.callbacks.add(cmnd, callback, precondition)

def add_example(cmnd, txt, example):
    if plugs:
        plugs.examples.add(cmnd, txt, example)

def add_bot(bot):
    if fleet:
        fleet.addbot(bot)

def add_reafter(index, regex, func, perms):
    if plugs:
        plugs.reafter.add(index, regex, func, perms)

def add_rebefore(index, regex, func, perms):
    if plugs:
        plugs.rebefore.add(index, regex, func, perms)

def add_alias(fromm, to):
    if plugs:
        plugs.aliases.data[fromm] = to

def get_datadir():
    if datadir:
        return datadir

def get_plugs():
    if plugs:
        return plugs

def get_users():
    if plugs:
        return plugs.users

def get_fleet():
    if fleet:
        return fleet

def get_cmnds():
    if plugs:
        return plugs.cmnds

def get_aliases():
    if plugs:
        return plugs.aliases

def get_config():
    if config:
        return config

def get_datadir():
    if datadir:
        return datadir

def get_examples():
    if plugs:
        return plugs.examples

def set_datadir(ddir):
    global datadir
    datadir = str(ddir)

def rlog(level, module, txt):
    if log:
        log.log(level, module, txt)
